<?php

declare (strict_types=1);

namespace App\Model;

use Hyperf\Database\Model\Events\Created;
use Hyperf\Database\Model\Relations\HasOne;

/**
 * @property int $id 主键
 * @property int $coupon_id 优惠券id
 * @property int $user_id 用户id
 * @property int $num 数量
 * @property \Carbon\Carbon $created_at 获得时间
 * @property int $get_type 1发放 2领取
 * @property \Carbon\Carbon $updated_at 修改时间
 * @property int $is_used 是否使用了
 * @property int $use_order_id
 */
class FlyCouponUser extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'fly_coupon_user';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'coupon_id' => 'integer', 'user_id' => 'string', 'num' => 'integer', 'created_at' => 'datetime', 'get_type' => 'integer', 'updated_at' => 'datetime', 'is_used' => 'integer', 'use_order_id' => 'integer'];

    public function coupon_info()
    {
        return $this->belongsTo(FlyCoupon::class, 'coupon_id', 'id')
            ->with('link_product');

    }

    /**
     * 关联有效期内优惠券
     * @return HasOne
     */
    public function with_coupon()
    {
        return $this->hasOne(FlyCoupon::class, 'id', 'coupon_id')
            ->with(['link_product'])
            ->where('use_begin_time', '<=', date('Y-m-d H:i:s', time()))
            ->where('use_end_time', '>=', date('Y-m-d', time()));
    }
}